package cn.mothin.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.mothin.entity.SystemRole;
import cn.mothin.frame.core.base.BaseDao;

@Repository("7d7910a9-eb4c-413c-99b4-f175a6fb658f")
public class SystemRoleDao extends BaseDao<SystemRole> {
    /**
     * 查询全部状态正常的角色信息列表
     * 
     * @return
     */
    public List<SystemRole> queryAll() {
        String sql = "SELECT * FROM system_role WHERE deleted<>TRUE ";
        return this.query(sql);
    }
    
    /**
     * 获取指定用的系统用户的角色信息
     * @param userId
     * @return
     */
    public List<SystemRole> queryUserRole(String userId){
        String sql = "SELECT * FROM system_role WHERE id IN (SELECT systemRoleId FROM system_user_role WHERE systemUserId=?)";
        return this.query(sql,userId);
    }
    
    /**
     * 查询指定用户全部的角色信息
     * @param userId
     * @return
     */
    public List<String> querySystemUserRoles(String userId){
        String sql = "SELECT systemRoleId FROM system_user_role WHERE systemUserId=?";
        return this.queryForStringList(sql,userId);
    }

    /**
     * 根据name检查是否已经存在未删除的角色信息
     * 
     * @param name
     * @return
     */
    public boolean checkExistedByName(String name) {
        String sql = "SELECT COUNT(0) FROM system_role WHERE roleName=? AND deleted=FALSE";
        return sqlHelper.queryCount(sql, name) > 0;
    }

    /**
     * 通过名字查找排除本身的角色信息
     * 
     * @param id
     * @param name
     * @return
     */
    public boolean checkExistedByNameWithOutId(String id, String name) {
        String hql = "SELECT COUNT(0) FROM system_role WHERE deleted<>TRUE AND id<>? AND roleName = ?";
        return sqlHelper.queryCount(hql, id, name) > 0;
    }
}
